/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.main.controller;

import com.kimstore.bean.Customer;
import com.kimstore.bean.Item;
import com.kimstore.bean.Order;
import com.kimstore.bean.Product;
import com.kimstore.factory.DaoFactory;
import com.kimstore.gui.customer.NewCustomerController;
import com.kimstore.gui.customer.ViewCustomerController;
import com.kimstore.gui.main.Inventory;
import com.kimstore.utilities.CustomTableRowSorter;
import com.kimstore.utilities.DateUtil;
import com.kimstore.utilities.PaginateSearchAndFilter;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.ref.Reference;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.decorator.HighlighterFactory;

/**
 *
 * @author Administrator
 */
public class CustomerTabController extends BaseTabController {

    private Inventory inventory;
    private PaginateSearchAndFilter<Customer> psaf;

    public CustomerTabController(Inventory inventory) {
        this.inventory = inventory;
    }

    public void populateCustomerTable() {
        populateCustomerTable(psaf.search());
    }

    private void populateCustomerTable(List<Customer> customers) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);
        for (Customer c : customers) {
            List<Item> item = DaoFactory.getInstance().getItemDao().getCustomerPurchases(c);

            model.insertRow(model.getRowCount(), new Object[]{c.getId(),
                        c.getName(),
                        (item != null && item.size() > 0) ? DateUtil.formatDate(item.get(0).getPurchaseDetail().getDateSold()) : "None"});

        }
    }

    public void clearTable() {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);
    }

    public void refreshTable() {
        clearTable();
        populateCustomerTable();
    }

    public void createNewCustomerController() {
        NewCustomerController controller = new NewCustomerController(this);
        controller.displayWindow();
    }

    public void createNewCustomerControllerCallback(Customer customer) {
        refreshTable();
    }

    public void createViewCustomerController(String id) {
        ViewCustomerController controller = new ViewCustomerController(this, id);
        controller.displayWindow();
    }

    public void createViewCustomerControllerCallback() {
        refreshTable();
        inventory.getOrdersTabController().refreshTable();
    }

    public Inventory getParentWindow() {
        return inventory;
    }

    @Override
    public void prepareUserInterface() {

        table.setShowGrid(true, true);
        table.addHighlighter(HighlighterFactory.createComputedForegroundHighlighter());
    }

    @Override
    public void bindElements() {
        table.setRowSorter(new CustomTableRowSorter(table.getRowSorter().getModel()) {
            @Override
            public void sort() {
                if (table.isShowing()) {
                    Point p = MouseInfo.getPointerInfo().getLocation();
                    int column = getSelectedColumn(p);
                    if (psaf.isColumnSortable(column)) {
                        populateCustomerTable(psaf.sort(column));
                    }
                }
            }
        });
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                populateCustomerTable(psaf.search());
            }
        });
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Customer> customers = psaf.nextPage();
                if (customers != null) {
                    populateCustomerTable(customers);
                }
            }
        });

        prevButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Customer> customers = psaf.previousPage();
                if (customers != null) {
                    populateCustomerTable(customers);
                }
            }
        });
    }

    private int getSelectedColumn(Point point) {
        int mousex = point.x - table.getLocationOnScreen().x;
        int mousey = point.y - table.getLocationOnScreen().y;
        Point mousep = new Point(mousex, mousey);

        return table.columnAtPoint(mousep);
    }

    public PaginateSearchAndFilter<Customer> getPsaf() {
        return psaf;
    }

    public void setPsaf(PaginateSearchAndFilter<Customer> psaf) {
        this.psaf = psaf;
    }
}
